package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func main() {
	//创建路由
	r :=  gin.Default()
	r.MaxMultipartMemory =8 <<20
	//绑定路由规则，执行函数
	//gin.context,封装requset和response
	r.GET("/upload", func(c *gin.Context) {
		_,headers,err :=  c.Request.FormFile("file")
		if err !=nil {
			log.Printf("Error when try to get file: %v", err)
		}
		//headers.Size 获取文件大小
		if headers.Size >1024*1024*2 {
			fmt.Println("文件太大了")
			return
		}
		if headers.Header.Get("Content-Type")!="image/png" {
			fmt.Println("只允许上传png图片")
			return
		}
		c.SaveUploadedFile(headers,"./video/"+headers.Filename)
		c.String(http.StatusOK,headers.Filename)
		//c.SaveUploadedFile(file,file.Filename)
		//c.String(http.StatusOK,file.Filename)
	})
	r.Run(":8200")

}
